<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use App\Models\Product;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Product>
 */
class ProductFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Product::class;

    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition()
    {
        // 随机选择供应商A或B
        $source = $this->faker->randomElement(['A', 'B']);

        return [
            'name' => $this->faker->name,
            'price' => $this->faker->randomFloat(2, 1, 1000),
            'source' => $source
        ];
    }

    /**
     * 生成供应商A的产品
     *
     * @return \Database\Factories\ProductFactory
     */
    public function sourceA()
    {
        return $this->state(function (array $attributes) {
            return [
                'source' => 'A',
            ];
        });
    }

    /**
     * 生成供应商B的产品
     *
     * @return \Database\Factories\ProductFactory
     */
    public function sourceB()
    {
        return $this->state(function (array $attributes) {
            return [
                'source' => 'B',
            ];
        });
    }

    /**
     * 生成低价产品
     *
     * @return \Database\Factories\ProductFactory
     */
    public function lowPrice()
    {
        return $this->state(function (array $attributes) {
            return [
                'price' => $this->faker->randomFloat(2, 1, 100),
            ];
        });
    }

    /**
     * 生成高价产品
     *
     * @return \Database\Factories\ProductFactory
     */
    public function highPrice()
    {
        return $this->state(function (array $attributes) {
            return [
                'price' => $this->faker->randomFloat(2, 500, 1000),
            ];
        });
    }
}